package com.dave.gd.core.common.globalsession.manager;

import java.util.Map;

import com.dave.gd.common.redis.impl.CacheServiceImpl;

public class SessionManager {
    
    private static final String GLOBALSESSION_INDEX_REDIS_KEY = "globalsession_index";
    private CacheServiceImpl    cacheService;
    
    public Map<String, String> getAllSessionIndex() {
        Map<String, String> indexs = cacheService.hgetAll(GLOBALSESSION_INDEX_REDIS_KEY);
        if (indexs == null || indexs.size() == 0) {
            return null;
        }
        return indexs;
    }
    
    public void addSessionIndex(String sessionId, String username) {
        cacheService.hset(GLOBALSESSION_INDEX_REDIS_KEY, sessionId, username);
    }
    
    public void deleteSessionIndex(String sessionId) {
        if (sessionId == null || sessionId.trim().length() == 0) {
            throw new IllegalArgumentException("sessionId should not be empty.");
        }
        cacheService.hdel(GLOBALSESSION_INDEX_REDIS_KEY, sessionId);
    }
    
    public boolean isSessionExists(String sessionId) {
        return cacheService.exists(sessionId);
    }
    
    public void killSession(String sessionId) {
        cacheService.del(sessionId);
        deleteSessionIndex(sessionId);
    }
    
}
